package com.epam.employeebase.entity;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Formula;

@Entity
@Table(name="offices")
public class Office {
	private Long id;
	private Address address;
	private Company company;
	private Set<Employment> employments;
	private Integer employeeCount;
	
	@Id
	@SequenceGenerator(name="officeGenerator", sequenceName="OFFICES_SEQ")
	@GeneratedValue(generator="officeGenerator")
	public Long getId() {
		return id;
	}
	
	@OneToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="address_id", unique=true)
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="company_id")
	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	
	public void setId(Long id) {
		this.id = id;
	}
	
	@OneToMany(mappedBy="office", fetch=FetchType.LAZY)
	public Set<Employment> getEmployments() {
		return employments;
	}

	public void setEmployments(Set<Employment> employments) {
		this.employments = employments;
	}
	
	@Formula(value="(SELECT COUNT(DISTINCT e.employee_id) FROM employments e WHERE e.office_id=id)")
	public Integer getEmployeeCount() {
		return employeeCount;
	}

	public void setEmployeeCount(Integer employeeCount) {
		this.employeeCount = employeeCount;
	}
}
